﻿var unsavedchanges = false;
var pendingCategoryinsert = false, pendingSubpageinsert = false, pendingAltLangpageinsert=false;
var currentpageId = 0;
var maxCategoryimportance = 0, maxSubpageimportance = 0;
var newtoplevelpage = false;
var currentpagetype = "";
var currentcatid = 0;
var currentlanguageid = 0;
var altLanguagePageId=0;

function LoadSubPageList(catid) {
    $("#isloading").show();
    var now = new Date();
    $.post(
	    "/backofficeAjax/GetSubPageList/" + catid + "/" + now.getMilliseconds(),
	    function(data) {
	        var objdata = JSON.parse(data);
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            PopulateSubPageList(data, true);
	        }
	    },
	    "json"
    );
}
function LoadCategoryPageList() {
    //alert("do some catlist shuffle stuff");
}

function PopulateSubPageList(data, isfirstload) {
    var objdata = JSON.parse(data);
    $("#SubPageList tbody").html("");
    var tblRow = "";
    var done = false;
    $.each(objdata, function(i, page) {
        if (page.ContentPageID == currentpageId) { cssclass = "currentpageinlist"; } else { cssclass = "pageinlist"; }
        tblRow += "<tr>";
        tblRow += "<td id='" + page.ContentPageID + "' class='" + cssclass + "' onclick='SelectPage(" + page.ContentPageID + ")'>" + page.Title + "</td><td></td>";
        tblRow += "</tr>";
        $(tblRow).appendTo("#SubPageList tbody");
        tblRow = "";
        maxSubpageimportance = page.Importance;
    });
    $("#isloading").hide();
}
function PopulateCategoryPageList(data) {
    var objdata = JSON.parse(data);
    $("#CategoryPageList tbody").html("");
    var iCellCnt = 0;
    var tblRow = "";
    var cssclass = ""
    var done = false;
    $.each(objdata, function(i, page) {
        if (page.ContentPageID == currentpageId) { cssclass = "currentpageinlist"; } else { cssclass = "pageinlist"; }
        tblRow += "<tr>";
        tblRow += "<td id='" + page.ContentPageID + "' class='" + cssclass + "' onclick='SelectPage(" + page.ContentPageID + ")'>" + page.Title + "</td><td></td>";
        tblRow += "</tr>";
        $(tblRow).appendTo("#CategoryPageList tbody");
        tblRow = "";
        maxCategoryimportance = page.Importance;
    });
}
function PopulateVersions(data) {
    var objdata = JSON.parse(data);
    $("#VersionPageList tbody").html("");
    var iCellCnt = 0;
    var tblRow = "";
    var cssclass = ""
    var done = false;
    $.each(objdata, function(i, page) {
        if (page.ContentPageID == currentpageId) { cssclass = "currentpageinlist"; } else { cssclass = "pageinlist"; }
        tblRow += "<tr>";
        tblRow += "<td id='" + page.ContentPageID + "' class='" + cssclass + "' onclick='SelectPage(" + page.ContentPageID + ")'>" + page.Title + "</td><td></td>";
        tblRow += "</tr>";
        $(tblRow).appendTo("#VersionPageList tbody");
        tblRow = "";
        maxCategoryimportance = page.Importance;
    });
}
function LoadFirstPage() {
    var now = new Date();
    $.post(
	    "/backofficeAjax/GetFirstCMSPage/0/" + now.getMilliseconds(),
	    function(data) {
	        var objdata = JSON.parse(data);
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            PopulatePageEditor(data);
	        }
	    },
	    "json"
    );
}
function LoadVersions(catid) {
    var now = new Date();
    $.post(
	    "/backofficeAjax/GetUnpublishedVersions/" + catid + "/" + now.getMilliseconds(),
	    function(data) {
	        var objdata = JSON.parse(data);
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            PopulateVersions(data);
	        }
	    },
	    "json"
    );
}
function PopulatePageEditor(data) {
    var page = JSON.parse(data);
    if (SwitchSelPage(page.ContentPageID)) {
        $("#Name").get(0).value = page.Title;
        $("#TargetDivsForWeightedContent").get(0).value = page.TargetDivsForWeightedContent;
        $("#Body").get(0).value = page.Body;
        
        editor.setData(page.Body);
        
        $("#Description").get(0).value = page.Description;
        $("#Summary").get(0).value = page.Summary;
        unsavedchanges = false;

        if (page.LanguageID != currentlanguageid) {
            $("#lang-" + currentlanguageid).removeClass("languageinlist-active");
            $("#lang-" + currentlanguageid).addClass("languageinlist");
            currentlanguageid = page.LanguageID;
            $("#lang-" + currentlanguageid).removeClass("languageinlist");
            $("#lang-" + currentlanguageid).addClass("languageinlist-active");            
        }

        var now = new Date();
        if (page.IsSubPage) {
            currentpagetype = "Sub";
            var encoded = JSON.stringify(page);
            $("#pageholder").val(encoded);
            if (allowPageWikiHtmlSwitch) {
                $("#subpage-options").load("/content/app_themes/backoffice/templates/subpageoptions-withWiki.html?time=" + now.getMilliseconds(), "", setvals);
            }
            else {
                $("#subpage-options").load("/content/app_themes/backoffice/templates/subpageoptions.html?time=" + now.getMilliseconds(), "", setvals);
            }
            $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-active");
            $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-altpages");
            $("#cat-lbl-" + currentcatid).addClass("currentcatinlist");
            $("#isloading").hide();
            $("#languages").show();
            altLanguagePageId = 0;      
        }
        else if (page.AltLanguageForPageID!=null) {
            currentpagetype = "AltLang";
            currentpageId = page.AltLanguageForPageID;
            altLanguagePageId = page.ContentPageID;
            $("#languages").show();         
        }
        else if (!page.IsSubPage && !page.IsCategoryPage) {
            currentpagetype = "Ver";
            $("#subpage-options").html("");            
            $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-active");
            $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-altpages");
            $("#" + currentpageId).removeClass("currentpageinlist-altpages");
            $("#cat-lbl-" + currentcatid).addClass("currentcatinlist");
            $("#isloading").hide();
            $("#languages").hide();
            altLanguagePageId = 0;
        }
        else {
            var encoded = JSON.stringify(page);
            $("#pageholder").val(encoded);
            if (allowPageWikiHtmlSwitch) {
                $("#subpage-options").load("/content/app_themes/backoffice/templates/catoptions-withWiki.html?time=" + now.getMilliseconds(), "", setcatvals);
            }
            else {
                $("#subpage-options").load("/content/app_themes/backoffice/templates/catoptions.html?time=" + now.getMilliseconds(), "", setcatvals);
            }
            currentpagetype = "Cat"
            $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-active");
            $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-altpages");
            $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist");
            $("#cat-lbl-" + currentcatid).addClass("pageinlist");
            currentcatid = page.CategoryID;
            $("#cat-lbl-" + currentcatid).removeClass("pageinlist");
            $("#cat-lbl-" + currentcatid).addClass("currentcatinlist-active");
            LoadVersions(page.CategoryID);
            LoadSubPageList(page.CategoryID);
            $("#subpage-options").html("");
            $("#languages").show();
            altLanguagePageId = 0;   
        }
    }
}
function setcatvals() {
    var page = JSON.parse($("#pageholder").val());
    if (page.ShowContributorLink) { $("#ShowContributorLink").attr("checked", true); }
    if (page.CommentsEnabled) { $("#CommentsEnabled").attr("checked", true); }
    if (page.AnonCommentsEnabled) { $("#AnonCommentsEnabled").attr("checked", true); }
    if (page.UseWikiMarkup) { $("#UseWikiMarkup").attr("checked", true); }        
    $("#UserID").val(page.ContributorID);
}
function setvals() {
    var page = JSON.parse($("#pageholder").val());
    if (page.Approved) { $("#Approved").attr("checked", true); }
    if (page.ShowContributorLink) { $("#ShowContributorLink").attr("checked", true); }
    if (page.OnlyForMembers) { $("#OnlyForMembers").attr("checked", true); }
    if (page.CommentsEnabled) { $("#CommentsEnabled").attr("checked", true); }
    if (page.AnonCommentsEnabled) { $("#AnonCommentsEnabled").attr("checked", true); }
    if (page.UseWikiMarkup) { $("#UseWikiMarkup").attr("checked", true); }    
    $("#UserID").val(page.ContributorID);
    
}
function SwitchSelPage(newid) {
    if (unsavedchanges) {
        continuewosave = confirm("You have unsaved changes.  Click OK to continue or Cancel to fishish changes.");
        if (!continuewosave) {
            return false;
        }
    }
    if (pendingSubpageinsert || pendingCategoryinsert) {
        if (pendingSubpageinsert) {
            DeleteNewDynamicPage(false);
            pendingDynamicinsert = false;
        }
        else {
            DeleteNewStaticPage(false);
            pendingCategoryinsert = false;
        }
    }
    else {
        $("#" + currentpageId).removeClass("currentpageinlist");
        $("#" + currentpageId).addClass("pageinlist");
    }
    currentpageId = newid;
    $("#" + currentpageId).removeClass("pageinlist");
    $("#" + currentpageId).addClass("currentpageinlist");
    $('#msgsuccess').html('');
    unsavedchanges = false;
    return true;
}
function DeleteNewStaticPage(checkswitch) {
    if (checkswitch) {
        if (!SwitchSelPage(0)) {
            return false;
        }
        else {
            $("#newpage").remove();
            LoadFirstPage();
            return true;
        }
    }
    $("#newpage").remove();
}
function DeleteNewDynamicPage(checkswitch) {
    if (checkswitch) {
        if (!SwitchSelPage(0)) {
            return false;
        }
        else {
            $("#newpage").remove();
            LoadFirstPage();
            return true;
        }
    }
    $("#newpage").remove();
}
function SelectCatPage(catid) {
    LoadCatPage(catid);
}
function LoadCatPage(catid) {
    $("#isloading").show();
    var now = new Date();
    $.post(
	    "/backofficeAjax/GetCatCMSPage/" + catid + "/" + now.getMilliseconds(),
	    function(data) {
	        var objdata = JSON.parse(data);
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            PopulatePageEditor(data);
	        }
	    },
	    "json"
    );
}
function SelectPage(pageid) {
    LoadPage(pageid);
}
function LoadPage(pageid) {
    $("#isloading").show();
    var now = new Date();
    $.post(
	    "/backofficeAjax/GetCMSPage/" + pageid + "/" + now.getMilliseconds(),
	    function(data) {
	        var objdata = JSON.parse(data);
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            PopulatePageEditor(data);
	        }
	    },
	    "json"
    );
}
function CategoryMenu(type) {
    switch (type) {
        case "plus":
            LoadCreateCategoryPage();
            newtoplevelpage = true;
            break;
        case "minus":
            if (currentpagetype == "Cat") { DeletePage(); }
            break;
        case "up":
            if (currentpagetype == "Cat") { ChangePageOrder(currentpageId, type, "Cat"); }
            break;
        case "down":
            if (currentpagetype == "Cat") { ChangePageOrder(currentpageId, type, "Cat"); }
            break;
        default:
    }
}
function SubPageMenu(type) {
    switch (type) {
        case "plus":
            LoadCreateSubPage(false);
            newtoplevelpage = true;            
            break;
        case "minus":
            if (currentpagetype == "Sub") { DeletePage(); }
            break;
        case "up":
            if (currentpagetype == "Sub") { ChangePageOrder(currentpageId, type, "Sub"); }
            break;
        case "down":
            if (currentpagetype == "Sub") { ChangePageOrder(currentpageId, type, "Sub"); }
            break;
        default:
    }
}
function VersionMenu(type) {
    switch (type) {
        case "plus":
            LoadCreateVersionPage(false);
            newtoplevelpage = true;
            break;
        case "minus":
            if (currentpagetype == "Ver") { DeletePage(); }
            break;
        default:
    }
}
function LoadCreateVersionPage() {
    if (SwitchSelPage(0)) {
        $("#Name").get(0).value = "New Page Name";

        editor.setData("New Page Content Goes Here");
        
        $("#Summary").get(0).value = "New Page Summary";
        $("#Description").get(0).value = "New Page Description";
        var tblRow = "";
        tblRow += "<tr id='newpage'>";
        tblRow += "<td id='0' class='currentpageinlist'>New Page</td><td><img class='cmsicon' onclick='DeleteNewDynamicPage(true)' title='cancel' src='../../Content/images/delicon.jpg' /></td>";
        tblRow += "</tr>";
        $(tblRow).appendTo("#VersionPageList tbody");
        $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-active");
        $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-altpages");
        $("#cat-lbl-" + currentcatid).addClass("currentcatinlist");
        unsavedchanges = true;
        pendingSubpageinsert = true;
        currentpagetype = "Ver";
        $("#subpage-options").html("");
    }
}
function LoadCreateSubPage() {
    var now = new Date();
    if (SwitchSelPage(0)) {
        $("#Name").get(0).value = "New Page Name";

        editor.setData("New Page Content Goes Here");
        
        $("#Summary").get(0).value = "New Page Summary";
        $("#Description").get(0).value = "New Page Description";
        var tblRow = "";
        tblRow += "<tr id='newpage'>";
        tblRow += "<td id='0' class='currentpageinlist'>New Page</td><td><img class='cmsicon' onclick='DeleteNewDynamicPage(true)' title='cancel' src='../../Content/images/delicon.jpg' /></td>";
        tblRow += "</tr>";
        $(tblRow).appendTo("#SubPageList tbody");
        unsavedchanges = true;
        pendingSubpageinsert = true;
        $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-active");
        $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-altpages");
        $("#cat-lbl-" + currentcatid).addClass("currentcatinlist");
        currentpagetype = "Sub";
        if (allowPageWikiHtmlSwitch) {
            $("#subpage-options").load("/content/app_themes/backoffice/templates/subpageoptions-withWiki.html?time=" + now.getMilliseconds(), "", setvals);
        }
        else {
            $("#subpage-options").load("/content/app_themes/backoffice/templates/subpageoptions.html?time=" + now.getMilliseconds(), "", setvals);
        }
    }
}
function LoadCreateCategoryPage() {
    var now = new Date();
    if (SwitchSelPage(0)) {
        $("#Name").get(0).value = "New Page Name";

        editor.setData("New Page Content Goes Here");
        
        $("#Summary").get(0).value = "New Page Summary";
        $("#Description").get(0).value = "New Page Description";
        var tblRow = "";
        tblRow += "<tr id='newpage'>";
        tblRow += "<td id='0' class='currentpageinlist'>New Page</td><td><img class='cmsicon' onclick='DeleteNewStaticPage(true)' title='cancel' src='../../Content/images/delicon.jpg' /></td>";
        tblRow += "</tr>";
        $(tblRow).appendTo("#CategoryPageList tbody");
        unsavedchanges = true;
        pendingCategoryinsert = true;
        currentpagetype = "Cat";
        if (allowPageWikiHtmlSwitch) {
            $("#subpage-options").load("/content/app_themes/backoffice/templates/catoptions-withWiki.html?time=" + now.getMilliseconds(), "", setcatvals);
        }
        else {
            $("#subpage-options").load("/content/app_themes/backoffice/templates/catoptions.html?time=" + now.getMilliseconds(), "", setcatvals);
        }                
    }
}
function SavePage() {
    var now = new Date();
    $("#isloading").show();
    $('#msgsuccess').html('');
    
    var bodyVal = editor.getData();    
    
    var issubpage = true, importance = 0, isCategoryPage = false;

    if (pendingSubpageinsert) {
        maxSubpageimportance += 1;
        importance = maxSubpageimportance;
    }
    if (pendingCategoryinsert) {
        maxCategoryimportance += 1;
        importance = maxCategoryimportance;
    }
    if (!(pendingSubpageinsert || pendingCategoryinsert)) {
        importance = 0;
    }
    if (currentpagetype == "Cat") {
        issubpage = false;
        isCategoryPage = true;
    }
    if (currentpagetype == "Ver" || currentpagetype == "AltLang") {
        issubpage = false;
    }
    var contribtr = { UserID: $("#UserID").val() }
    var appr, shcnt, ofm, ce, ace, uwm;
    var altLanguageForPageID = null;
    if (currentpagetype == "AltLang") {
        altLanguageForPageID = currentpageId;
        if (pendingAltLangpageinsert) {
            currentpageId = 0;
        }
        else {
            currentpageId = altLanguagePageId;
        }
    }

    if ($("#Approved:checked").val() == 'on') { appr = true; } else { appr = false; }
    if ($("#ShowContributorLink:checked").val() == 'on') { shcnt = true; } else { shcnt = false; }
    if ($("#OnlyForMembers:checked").val() == 'on') { ofm = true; } else { ofm = false; }
    if ($("#CommentsEnabled:checked").val() == 'on') { ce = true; } else { ce = false; }
    if ($("#AnonCommentsEnabled:checked").val() == 'on') { ace = true; } else { ace = false; }
    if ($("#UseWikiMarkup:checked").val() == 'on') { uwm = true; } else { uwm = false; }
    var parentLanguageVersionData = "";

    var parentLanguageVersionData = {
        ContentPageID: altLanguageForPageID
    }

    var languageData = {
        LanguageID: currentlanguageid 
    }    
    var data;
    if (currentpagetype == "AltLang") {
        data = {
            ContentPageid: currentpageId,
            Title: $('#Name').val(),
            Description: $('#Description').val(),
            Body: bodyVal,
            IsSubPage: issubpage,
            Importance: importance,
            Summary: $('#Summary').val(),
            IsCategoryPage: isCategoryPage,
            Approved: appr,
            OnlyForMembers: ofm,
            CommentsEnabled: ce,
            AnonCommentsEnabled: ace,
            UseWikiMarkup: uwm,
            Contributor: contribtr,
            Language: languageData,
            ParentLanguageVersion: parentLanguageVersionData,
            ShowContributorLink: shcnt,
            TargetDivsForWeightedContent: $("#TargetDivsForWeightedContent").val()
        }
    }
    else {
        data = {
            ContentPageid: currentpageId,
            Title: $('#Name').val(),
            Description: $('#Description').val(),
            Body: bodyVal,
            IsSubPage: issubpage,
            Importance: importance,
            Summary: $('#Summary').val(),
            IsCategoryPage: isCategoryPage,
            Approved: appr,
            OnlyForMembers: ofm,
            CommentsEnabled: ce,
            AnonCommentsEnabled: ace,
            UseWikiMarkup: uwm,
            Contributor: contribtr,
            Language: languageData,
            ShowContributorLink: shcnt,
            TargetDivsForWeightedContent: $("#TargetDivsForWeightedContent").val()
        }
    }
    var encoded = JSON.stringify(data);
    $.post(
	    "/backofficeAjax/SavePage/" + currentcatid + "/" + issubpage,
		{ data: encoded },
	    function(data) {
	        var objdata = data;
	        if (objdata.error) {
	            alert(objdata.error);
	            $("#isloading").hide();
	        }
	        else {
	            $("#isloading").hide();
	            $('#msgsuccess').html('The page was saved.');
	            if (objdata.AltLanguageForPageID == 0) {
	                currentpageId = objdata.ContentPageID;
	            }
	            else {
	                currentpageId = objdata.AltLanguageForPageID;
	                altLanguagePageId = objdata.ContentPageID;
	            }
	            newtoplevelpage = false;
	            unsavedchanges = false;
	            pendingSubpageinsert = false;
	            pendingAltLangpageinsert = false;
	            pendingCategoryinsert = false;
	            if (objdata.IsSubPage) {
	                LoadSubPageList(currentcatid);
	            }
	            else {
	                if (currentpagetype == "Ver") {
	                    LoadVersions(currentcatid);
	                }
	            }
	        }
	    },
	    "json"
    );
}
function ChangePageOrder(pageid, direction, pagetype) {
    var now = new Date();
    if (pageid == 0) {
        alert("Save the new page before changing its order");
        return false;
    }
    $.post(
	    "/backofficeAjax/ChangePageOrder" + direction + "/" + pageid + "/" + now.getMilliseconds(),
	    function(data) {
	        var objdata = JSON.parse(data);
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            if (pagetype == "Cat") {
	                window.location.href = "cms";
	            }
	            else {
	                LoadSubPageList(objdata.CategoryID);
	            }
	        }
	    },
	    "json"
    );
}
function DeletePage() {
    if (newtoplevelpage) {
        alert("To delete the 'New Page' click the red x next to it in the list");
        return false;
    }
    continuedelete = confirm("Are you sure you want to delete the current page? ");
    if (!continuedelete) {
        return false;
    }
    var now = new Date();
    $.post(
	    "/backofficeAjax/DeletePage/" + currentpageId + "/" + now.getMilliseconds(),
	    function(data) {
	        var objdata = JSON.parse(data);
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            newtoplevelpage = false;
	            unsavedchanges = false;
	            pendingSubpageinsert = false;
	            pendingCategoryinsert = false;
	            SelectCatPage(currentcatid)
	            if (objdata.IsSubPage) {
	                LoadSubPageList(currentcatid);
	            }
	            else {
	                LoadVersions(currentcatid);
	            }
	        }
	    },
	    "json"
    );
}
function SetVersion() {
    window.location.href = "setpublishedpage/" + currentcatid;
}
function ChangeLanguage(languageId) {
    var defaultLanguageId = $("#defaultLanguageId").val();
    if (unsavedchanges && defaultLanguageId != languageId) {
        continuewosave = confirm("You have unsaved changes.  Click OK to continue or Cancel to fishish changes.");
        if (!continuewosave) {
            return false;
        }
        else {
            unsavedchanges = false;
        }
    }   
    
    $("#lang-" + currentlanguageid).removeClass("languageinlist-active");
    $("#lang-" + currentlanguageid).addClass("languageinlist");
    currentlanguageid = languageId;
    $("#lang-" + currentlanguageid).removeClass("languageinlist");
    $("#lang-" + currentlanguageid).addClass("languageinlist-active");
    
    if (defaultLanguageId == languageId) {
        LoadPage(currentpageId);
    }
    else {
        LoadAltLangPage(languageId);
    }
    //todo maybe not if it is the portal default
    currentpagetype = "AltLang";
    $("#isloading").hide();
    $("#cat-lbl-" + currentcatid).removeClass("currentcatinlist-active");
    $("#cat-lbl-" + currentcatid).addClass("currentcatinlist-altpages");
    $("#" + currentpageId).removeClass("currentpageinlist");
    $("#" + currentpageId).addClass("currentpageinlist-altpages");      
}

function LoadAltLangPage(languageId) {
    $("#isloading").show();
    var now = new Date();
    var data = {
        ParentPageID: currentpageId,
        LanguageID: languageId
    }
    var encoded = JSON.stringify(data);
    $.post(
	    "/backofficeAjax/GetAltLangPage/0/" + now.getMilliseconds(),
	    { data: encoded },
	    function(data) {
	        var objdata = data;
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            PopulateAltLangPage(data);
	        }
	    },
	    "json"
    );
}
function PopulateAltLangPage(data) {
    if (data.exists==false) {
        $("#Name").get(0).value = "New Alternate Language Page Name";

        editor.setData("New Alternate Language Page Content Goes Here");
        
        $("#Summary").get(0).value = "New Page Summary";
        $("#Description").get(0).value = "New Alternate Language Page Description";
        $('#msgsuccess').html('');
        unsavedchanges = true;
        pendingAltLangpageinsert = true;        
    }
    else {
        var page = JSON.stringify(data);
        PopulatePageEditor(page);
    }

}
function DeleteAltPage(langid) {

    continuedelete = confirm("Are you sure you want to delete the current page? ");
    if (!continuedelete) {
        return false;
    }
    if (currentlanguageid != langid) {
        alert("Please select the page to delete before deleting");
        return false;
    }
    if (altLanguagePageId == 0) {
        alert("There is no page defined for this alt language");
        return false;
    }
    var now = new Date();
    $.post(
	    "/backofficeAjax/DeletePage/" + altLanguagePageId + "/" + now.getMilliseconds(),
	    function(data) {
	        var objdata = JSON.parse(data);
	        if (objdata.error) {
	            alert(objdata.error);
	        }
	        else {
	            newtoplevelpage = false;
	            unsavedchanges = false;
	            pendingSubpageinsert = false;
	            pendingCategoryinsert = false;
	            SelectCatPage(currentcatid)
	            if (objdata.IsSubPage) {
	                LoadSubPageList(currentcatid);
	            }
	            else {
	                LoadVersions(currentcatid);
	            }
	        }
	    },
	    "json"
    );
}